﻿using DataGridDemo.Models;
using System.Collections.Generic;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;

namespace DataGridDemo
{
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();

            var userModels = new List<UserModel>
            {
                new UserModel() { Id = 1, Name = "John Doe" },
                new UserModel() { Id = 2, Name = "Jane Doe" },
                new UserModel() { Id = 3, Name = "Sammy Doe" }
            };

            datagrid.ItemsSource = userModels;
        }

        /// <summary>
        /// 设置行号，有效
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void DataGrid_LoadingRow(object sender, DataGridRowEventArgs e)
        {
            e.Row.Header = (e.Row.GetIndex() + 1).ToString();
        }

        /// <summary>
        /// 该方法因C占用，不能进入，换掉C成其他可以进入，进入后取值有效赋值到粘贴板中
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void DataGrid_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyboardDevice.Modifiers == ModifierKeys.Control && e.Key == Key.S)
            {
                if (datagrid.CurrentCell != null)
                {
                    var userModel = datagrid.CurrentCell.Item as UserModel;
                    e.Handled = true;
                    var value = typeof(UserModel).GetProperty(datagrid.CurrentCell.Column.SortMemberPath).GetValue(userModel, null);
                    MessageBox.Show(value.ToString());
                    Clipboard.SetText(value.ToString());
                }
            }
        }

        /// <summary>
        /// 按照当前选中单元格所在列取到当前行数据，有效
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void DataGrid_CopyingRowClipboardContent(object sender, DataGridRowClipboardEventArgs e)
        {
            var test = e.ClipboardRowContent[datagrid.CurrentCell.Column.DisplayIndex];
            e.ClipboardRowContent.Clear();
            e.ClipboardRowContent.Add(test);
        }
    }
}
